/**
 * 作者: wgx
 * 日期: 2025-01-17 11:53:49
 * 备注: 用户主线关卡信息表(避外探险)
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserStage;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserStageModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected int mapId; // 当前在打的章节Id
    protected int stageId; // 当前在打的关卡id
    protected int nodeNum; // 小节奖励的领取进度
    protected int unlockAreaId; // 已解锁的最大区域id (unlock_area_id-1表示已清理的区域)
    protected int inspireNum; // 金币鼓舞次数
    protected int inspireItemNum; // 道具鼓舞的次数
    protected String nodeReward = ""; // 小节奖励
    protected String areaReward = ""; // 区域奖励领取记录
    protected long guardRewardTime; // 结算卫兵挂机奖励的时间
    protected long guardRewardRandomTime; // 结算卫兵挂机随机奖励的时间
    protected String guardReward = ""; // 卫兵挂机奖励内容
    protected long resetTime; // 每日重置时间
    protected int guardQuickNum; // 卫兵挂机奖励额外今日领取的次数 (每日重置)
    protected long guardRewardLastTime; // 上一次领取卫兵挂机奖励的时间
    protected int guardCloseTime; // 挂机奖励已经给的时长单位秒

    public void reset() {
    }

    protected String getKey() {
        return "UserStage." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_stage"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("mapId", mapId); // 当前在打的章节Id
        obj.put("stageId", stageId); // 当前在打的关卡id
        obj.put("nodeNum", nodeNum); // 小节奖励的领取进度
        obj.put("unlockAreaId", unlockAreaId); // 已解锁的最大区域id (unlock_area_id-1表示已清理的区域)
        obj.put("inspireNum", inspireNum); // 金币鼓舞次数
        obj.put("inspireItemNum", inspireItemNum); // 道具鼓舞的次数
        obj.put("nodeReward", nodeReward); // 小节奖励
        obj.put("areaReward", areaReward); // 区域奖励领取记录
        obj.put("guardRewardTime", guardRewardTime); // 结算卫兵挂机奖励的时间
        obj.put("guardRewardRandomTime", guardRewardRandomTime); // 结算卫兵挂机随机奖励的时间
        obj.put("guardReward", guardReward); // 卫兵挂机奖励内容
        obj.put("resetTime", resetTime); // 每日重置时间
        obj.put("guardQuickNum", guardQuickNum); // 卫兵挂机奖励额外今日领取的次数 (每日重置)
        obj.put("guardRewardLastTime", guardRewardLastTime); // 上一次领取卫兵挂机奖励的时间
        obj.put("guardCloseTime", guardCloseTime); // 挂机奖励已经给的时长单位秒
        return obj;
    }

    public PBUserStage.Builder takeInitBuilder() {
        PBUserStage.Builder sb = PBUserStage.newBuilder();
        if (mapId != 0) {
            sb.setMapId(mapId);
        }
        if (stageId != 0) {
            sb.setStageId(stageId);
        }
        if (nodeNum != 0) {
            sb.setNodeNum(nodeNum);
        }
        if (unlockAreaId != 0) {
            sb.setUnlockAreaId(unlockAreaId);
        }
        if (inspireNum != 0) {
            sb.setInspireNum(inspireNum);
        }
        if (inspireItemNum != 0) {
            sb.setInspireItemNum(inspireItemNum);
        }
        if (this.nodeReward != null && this.nodeReward.length() > 0) {
            JsonArray list = new JsonArray(this.nodeReward);
            for(int i=0; i<list.size(); i++) {
                sb.addNodeReward(list.getLong(i));
            }
        }
        if (this.areaReward != null && this.areaReward.length() > 0) {
            JsonArray list = new JsonArray(this.areaReward);
            for(int i=0; i<list.size(); i++) {
                sb.addAreaReward(list.getInteger(i));
            }
        }
        if (guardRewardTime != 0) {
            sb.setGuardRewardTime(guardRewardTime);
        }
        if (!guardReward.isEmpty()) {
            sb.setGuardReward(guardReward);
        }
        if (guardQuickNum != 0) {
            sb.setGuardQuickNum(guardQuickNum);
        }
        if (guardRewardLastTime != 0) {
            sb.setGuardRewardLastTime(guardRewardLastTime);
        }
        return sb;
    }

    public PBUserStage.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserStage.Builder sb = PBUserStage.newBuilder();
        if (isFieldUpdate("mapId")) {
            sb.setMapId(mapId);
        }
        if (isFieldUpdate("stageId")) {
            sb.setStageId(stageId);
        }
        if (isFieldUpdate("nodeNum")) {
            sb.setNodeNum(nodeNum);
        }
        if (isFieldUpdate("unlockAreaId")) {
            sb.setUnlockAreaId(unlockAreaId);
        }
        if (isFieldUpdate("inspireNum")) {
            sb.setInspireNum(inspireNum);
        }
        if (isFieldUpdate("inspireItemNum")) {
            sb.setInspireItemNum(inspireItemNum);
        }
        if (isFieldUpdate("nodeReward")) {
            if (this.nodeReward != null && this.nodeReward.length() > 0) {
                JsonArray list = new JsonArray(this.nodeReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addNodeReward(list.getLong(i));
                }
                if (list.isEmpty()) {
                    sb.clearNodeReward();
                }
            } else {
                sb.clearNodeReward();
            }
        }
        if (isFieldUpdate("areaReward")) {
            if (this.areaReward != null && this.areaReward.length() > 0) {
                JsonArray list = new JsonArray(this.areaReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addAreaReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearAreaReward();
                }
            } else {
                sb.clearAreaReward();
            }
        }
        if (isFieldUpdate("guardRewardTime")) {
            sb.setGuardRewardTime(guardRewardTime);
        }
        if (isFieldUpdate("guardReward")) {
            sb.setGuardReward(guardReward);
        }
        if (isFieldUpdate("guardQuickNum")) {
            sb.setGuardQuickNum(guardQuickNum);
        }
        if (isFieldUpdate("guardRewardLastTime")) {
            sb.setGuardRewardLastTime(guardRewardLastTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getMapId() {
        return mapId;
    }

    public void putMapId(int mapId) {
        if (this.mapId != mapId) {
            this.mapId = mapId;
            updateRedisHashField(getKey(), "mapId", this.mapId);
        }
    }

    public void addMapId(int value) {
        this.mapId += value;
        updateRedisHashField(getKey(), "mapId", this.mapId);
    }

    public int getStageId() {
        return stageId;
    }

    public void putStageId(int stageId) {
        if (this.stageId != stageId) {
            this.stageId = stageId;
            updateRedisHashField(getKey(), "stageId", this.stageId);
        }
    }

    public void addStageId(int value) {
        this.stageId += value;
        updateRedisHashField(getKey(), "stageId", this.stageId);
    }

    public int getNodeNum() {
        return nodeNum;
    }

    public void putNodeNum(int nodeNum) {
        if (this.nodeNum != nodeNum) {
            this.nodeNum = nodeNum;
            updateRedisHashField(getKey(), "nodeNum", this.nodeNum);
        }
    }

    public void addNodeNum(int value) {
        this.nodeNum += value;
        updateRedisHashField(getKey(), "nodeNum", this.nodeNum);
    }

    public int getUnlockAreaId() {
        return unlockAreaId;
    }

    public void putUnlockAreaId(int unlockAreaId) {
        if (this.unlockAreaId != unlockAreaId) {
            this.unlockAreaId = unlockAreaId;
            updateRedisHashField(getKey(), "unlockAreaId", this.unlockAreaId);
        }
    }

    public void addUnlockAreaId(int value) {
        this.unlockAreaId += value;
        updateRedisHashField(getKey(), "unlockAreaId", this.unlockAreaId);
    }

    public int getInspireNum() {
        return inspireNum;
    }

    public void putInspireNum(int inspireNum) {
        if (this.inspireNum != inspireNum) {
            this.inspireNum = inspireNum;
            updateRedisHashField(getKey(), "inspireNum", this.inspireNum);
        }
    }

    public void addInspireNum(int value) {
        this.inspireNum += value;
        updateRedisHashField(getKey(), "inspireNum", this.inspireNum);
    }

    public int getInspireItemNum() {
        return inspireItemNum;
    }

    public void putInspireItemNum(int inspireItemNum) {
        if (this.inspireItemNum != inspireItemNum) {
            this.inspireItemNum = inspireItemNum;
            updateRedisHashField(getKey(), "inspireItemNum", this.inspireItemNum);
        }
    }

    public void addInspireItemNum(int value) {
        this.inspireItemNum += value;
        updateRedisHashField(getKey(), "inspireItemNum", this.inspireItemNum);
    }

    public JsonArray getNodeRewardArray() {
        if (this.nodeReward != null && this.nodeReward.length() > 0) {
            return new JsonArray(this.nodeReward);
        } else {
            return new JsonArray();
        }
    }

    public String getNodeReward() {
        return nodeReward;
    }

    public void putNodeReward(String nodeReward) {
        if (!this.nodeReward.equals(nodeReward)) {
            this.nodeReward = nodeReward;
            updateRedisHashField(getKey(), "nodeReward", this.nodeReward);
        }
    }

    public JsonArray getAreaRewardArray() {
        if (this.areaReward != null && this.areaReward.length() > 0) {
            return new JsonArray(this.areaReward);
        } else {
            return new JsonArray();
        }
    }

    public String getAreaReward() {
        return areaReward;
    }

    public void putAreaReward(String areaReward) {
        if (!this.areaReward.equals(areaReward)) {
            this.areaReward = areaReward;
            updateRedisHashField(getKey(), "areaReward", this.areaReward);
        }
    }

    public long getGuardRewardTime() {
        return guardRewardTime;
    }

    public void putGuardRewardTime(long guardRewardTime) {
        if (this.guardRewardTime != guardRewardTime) {
            this.guardRewardTime = guardRewardTime;
            updateRedisHashField(getKey(), "guardRewardTime", this.guardRewardTime);
        }
    }

    public void addGuardRewardTime(long value) {
        this.guardRewardTime += value;
        updateRedisHashField(getKey(), "guardRewardTime", this.guardRewardTime);
    }

    public long getGuardRewardRandomTime() {
        return guardRewardRandomTime;
    }

    public void putGuardRewardRandomTime(long guardRewardRandomTime) {
        if (this.guardRewardRandomTime != guardRewardRandomTime) {
            this.guardRewardRandomTime = guardRewardRandomTime;
            updateRedisHashField(getKey(), "guardRewardRandomTime", this.guardRewardRandomTime);
        }
    }

    public void addGuardRewardRandomTime(long value) {
        this.guardRewardRandomTime += value;
        updateRedisHashField(getKey(), "guardRewardRandomTime", this.guardRewardRandomTime);
    }

    public String getGuardReward() {
        return guardReward;
    }

    public void putGuardReward(String guardReward) {
        if (!this.guardReward.equals(guardReward)) {
            this.guardReward = guardReward;
            updateRedisHashField(getKey(), "guardReward", this.guardReward);
        }
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public int getGuardQuickNum() {
        return guardQuickNum;
    }

    public void putGuardQuickNum(int guardQuickNum) {
        if (this.guardQuickNum != guardQuickNum) {
            this.guardQuickNum = guardQuickNum;
            updateRedisHashField(getKey(), "guardQuickNum", this.guardQuickNum);
        }
    }

    public void addGuardQuickNum(int value) {
        this.guardQuickNum += value;
        updateRedisHashField(getKey(), "guardQuickNum", this.guardQuickNum);
    }

    public long getGuardRewardLastTime() {
        return guardRewardLastTime;
    }

    public void putGuardRewardLastTime(long guardRewardLastTime) {
        if (this.guardRewardLastTime != guardRewardLastTime) {
            this.guardRewardLastTime = guardRewardLastTime;
            updateRedisHashField(getKey(), "guardRewardLastTime", this.guardRewardLastTime);
        }
    }

    public void addGuardRewardLastTime(long value) {
        this.guardRewardLastTime += value;
        updateRedisHashField(getKey(), "guardRewardLastTime", this.guardRewardLastTime);
    }

    public int getGuardCloseTime() {
        return guardCloseTime;
    }

    public void putGuardCloseTime(int guardCloseTime) {
        if (this.guardCloseTime != guardCloseTime) {
            this.guardCloseTime = guardCloseTime;
            updateRedisHashField(getKey(), "guardCloseTime", this.guardCloseTime);
        }
    }

    public void addGuardCloseTime(int value) {
        this.guardCloseTime += value;
        updateRedisHashField(getKey(), "guardCloseTime", this.guardCloseTime);
    }

}